<template>
  <a-modal
    title="编辑"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id']"/>
        </a-form-item>
        <a-form-item label="商品名">
          <a-select v-decorator="['goodId']" show-search placeholder="请选择" >
            <a-select-option :value="i.id" v-for="i in this.prodOption" :key="i.value">{{ i.name }} </a-select-option
            >
          </a-select>
        </a-form-item>
        <a-form-item label="成本">
          <a-input
            v-decorator="['commCost']"
            addonAfter="元"
            name="commCost"
            placeholder="请输入成本"
          />
        </a-form-item>
        <a-form-item label="供应商往来账">
          <a-radio-group v-decorator="['isRecord',{ initialValue: 1 , rules: [{ required: true, message: '请选择' }]}]" placeholder="请选择">
            <a-radio :style="radioStyle" :value="1">记录</a-radio>
            <a-radio :style="radioStyle" :value="2">不记录</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="串码">
          <a-input
            v-decorator="['phoneCode1']"
            name="phoneCode1"
            placeholder="请输入串码"
          />
        </a-form-item>
        <a-form-item label="串码2">
          <a-input
            v-decorator="['phoneCode2']"
            name="phoneCode2"
            placeholder="请输入串码2"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getGoodsListUsingPOST } from '@/api/api/shangpinguanli.ts'
// 表单字段
const fields = ['goodId', 'commCost','phoneCode1','phoneCode2','id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      radioStyle: {
        display: 'block',
        height: '30px',
        lineHeight: '30px',
      },
      prodOption: [],
      queryParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      form: this.$form.createForm(this),
    }
  },
  created() {
    this.handleProd()
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods:{
    handleProd() {
      getGoodsListUsingPOST({...this.queryParam})
        .then((res) => {
          this.prodOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  }
}
</script>
